package com.zzz.robot201.mvp.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.zzz.robot201.R;
import com.zzz.robot201.di.component.DaggerLeftForelegComponent;
import com.zzz.robot201.mvp.contract.LeftForelegContract;
import com.zzz.robot201.mvp.presenter.LeftForelegPresenter;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.zzz.robot201.mvp.ui.activity.HomeActivity.responseMsg;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/13/2020 02:26
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class LeftForelegFragment extends BaseFragment<LeftForelegPresenter> implements LeftForelegContract.View {

    @BindView(R.id.tv_sqb)
    TextView tvSqb;
    @BindView(R.id.tv_xqb)
    TextView tvXqb;
    @BindView(R.id.tv_xgj)
    TextView tvXgj;
    @BindView(R.id.tv_sqb_lj_qw)
    TextView tvSqbLjQw;
    @BindView(R.id.tv_xqb_lj_qw)
    TextView tvXqbLjQw;
    @BindView(R.id.tv_xgj_lj_wq)
    TextView tvXgjLjWq;
    @BindView(R.id.tv_sqb_lj)
    TextView tvSqbLj;
    @BindView(R.id.tv_xqb_lj)
    TextView tvXqbLj;
    @BindView(R.id.tv_xgj_lj)
    TextView tvXgjLj;
    @BindView(R.id.tv_sqb_qw)
    TextView tvSqbQw;
    @BindView(R.id.tv_xqb_qw)
    TextView tvXqbQw;
    @BindView(R.id.tv_xgj_qw)
    TextView tvXgjQw;
    @BindView(R.id.tv_sqb_cgq)
    TextView tvSqbCgq;
    @BindView(R.id.tv_xqb_cgq)
    TextView tvXqbCgq;
    @BindView(R.id.tv_xgj_cgq)
    TextView tvXgjCgq;
    private Timer timer;
    public static LeftForelegFragment newInstance() {
        LeftForelegFragment fragment = new LeftForelegFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerLeftForelegComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_left_foreleg, container, false);
    }

    private int count = 1;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    updateUi();
                    break;

                default:
                    break;
            }
        }

        ;
    };

    private void updateUi() {
        if (responseMsg!=null) {
            //实际角度
            tvSqb.setText(responseMsg.m_q[0][0] + "");
            tvXqb.setText(responseMsg.m_q[0][1] + "");
            tvXgj.setText(responseMsg.m_q[0][2] + "");
            //期望力矩
            tvSqbLjQw.setText(responseMsg.m_tau_d[0][0] + "");
            tvXqbLjQw.setText(responseMsg.m_tau_d[0][1] + "");
            tvXgjLjWq.setText(responseMsg.m_tau_d[0][2] + "");
            //实际力矩
            tvSqbLj.setText(responseMsg.m_tau[0][0] + "");
            tvXqbLj.setText(responseMsg.m_tau[0][1] + "");
            tvXgjLj.setText(responseMsg.m_tau[0][2] + "");
            //期望角度
//            tvSqbQw.setText(responseMsg.m_q_d[0][0] + "");
//            tvXqbQw.setText(responseMsg.m_q_d[0][1] + "");
//            tvXgjQw.setText(responseMsg.m_q_d[0][2] + "");

            tvSqbQw.setText((Math.round((responseMsg.m_q_d[3][0])*100))/100 + "");
            tvXqbQw.setText((Math.round((responseMsg.m_q_d[3][1])*100))/100 + "");
            tvXgjQw.setText((Math.round((responseMsg.m_q_d[3][3])*100))/100 + "");
            //传感器状态
            //tvSqbCgq.setText(responseMsg.sensor_diag_char[0][0] + "");
            //tvXqbCgq.setText(responseMsg.sensor_diag_char[0][1] + "");
            //tvXgjCgq.setText(responseMsg.sensor_diag_char[0][2] + "");
            if(responseMsg.sensor_diag_char[0][0] == 0){
                tvSqbCgq.setText("关节正常");
                tvSqbCgq.setTextColor(getResources().getColor(R.color.green));
            }else if(responseMsg.sensor_diag_char[0][0] == 1){
                tvSqbCgq.setText("位置异常");
                tvSqbCgq.setTextColor(getResources().getColor(R.color.red));
            }else if(responseMsg.sensor_diag_char[0][0] == 2){
                tvSqbCgq.setText("电流异常");
                tvSqbCgq.setTextColor(getResources().getColor(R.color.red));
            }
            if(responseMsg.sensor_diag_char[0][1] == 0){
                tvXqbCgq.setText("关节正常");
                tvXqbCgq.setTextColor(getResources().getColor(R.color.green));
            }else if(responseMsg.sensor_diag_char[0][1] == 1){
                tvXqbCgq.setText("位置异常");
                tvXqbCgq.setTextColor(getResources().getColor(R.color.red));
            }else if(responseMsg.sensor_diag_char[0][1] == 2){
                tvXqbCgq.setText("电流异常");
                tvXqbCgq.setTextColor(getResources().getColor(R.color.red));
            }
            if(responseMsg.sensor_diag_char[0][2] == 0){
                tvXgjCgq.setText("关节正常");
                tvXgjCgq.setTextColor(getResources().getColor(R.color.green));
            }else if(responseMsg.sensor_diag_char[0][2] == 1){
                tvXgjCgq.setText("位置异常");
                tvXgjCgq.setTextColor(getResources().getColor(R.color.red));
            }else if(responseMsg.sensor_diag_char[0][2] == 2){
                tvXgjCgq.setText("电流异常");
                tvXgjCgq.setTextColor(getResources().getColor(R.color.red));
            }
        }
    }


    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);

            }
        }, 1000, 3000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    /**
     * 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
     * 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * <p>
     * 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
     * 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
     * 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
     * <p>
     * Example usage:
     * <pre>
     * public void setData(@Nullable Object data) {
     *     if (data != null && data instanceof Message) {
     *         switch (((Message) data).what) {
     *             case 0:
     *                 loadData(((Message) data).arg1);
     *                 break;
     *             case 1:
     *                 refreshUI();
     *                 break;
     *             default:
     *                 //do something
     *                 break;
     *         }
     *     }
     * }
     *
     * // call setData(Object):
     * Message data = new Message();
     * data.what = 0;
     * data.arg1 = 1;
     * fragment.setData(data);
     * </pre>
     *
     * @param data 当不需要参数时 {@code data} 可以为 {@code null}
     */
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }
}
